package com.situ.day8;

import java.io.BufferedWriter;

import org.junit.Test;

import com.situ.day7.Student;

public class StringDemo {
	@Test
	public void test1() {
		// 如果常量池中没有"abc"这个字符串，就在常量池中new一个字符串
		String str1 = "abc";
		String str2 = new String("abc");
		//在常量池中有"abc"这样一个字符串，就直接指向常量池中已有的字符串
		String str3 = "abc";
		System.out.println(str1 == str2);//false
		System.out.println(str1 == str3);//true
		System.out.println(str2 == str3);//false
	}
	
	@Test
	public void test2() {
		String str1 = new String("abc");
		String str2 = new String("Abc");
		System.out.println(str1 == str2);// false
		System.out.println(str1.equals(str2));// false
		System.out.println(str1.equalsIgnoreCase(str2));// true
	}
	
	@Test
	public void test111() {
		//
		int str1 = 5;
		Integer str2 = new Integer(5);
		Integer str3 = 5;
		//
		System.out.println(str1 == str2);// true
		System.out.println(str1 == str3);// true
		System.out.println(str2 == str3);// false
	}
	
	@Test
	public void test3() {
		String str = "java AndroidA";
		System.out.println(str.length());
		char[] array = str.toCharArray();
		char ch = str.charAt(5);
		System.out.println(ch);// A
		
		System.out.println(str.indexOf('A'));// 5
		//如果为出现改字符，则返回-1
		System.out.println(str.indexOf('X'));// -1
		System.out.println(str.indexOf('A', 6));// 12
		System.out.println(str.indexOf("And"));// 5
		
		System.out.println(str.lastIndexOf('A'));// 12
	}

	@Test
	public void test22() {
		String str = "java AndroidA";
		System.out.println(str.contains("And"));//true
		System.out.println(str.startsWith("java"));//true
		System.out.println(str.startsWith("And", 5));//true
		System.out.println(str.endsWith("oidA"));//true
		System.out.println(str.isEmpty());//false
		System.out.println("".isEmpty());//true
	}
	
	@Test
	public void test455() {
		//String str = "java And"; 
		char[] array = {'j', 'a', 'v', 'a', ' ', 'A', 'n', 'd'};
		String str = new String(array);
		System.out.println(str);
		char[] charArray = str.toCharArray();
		for (char c : charArray) {
			System.out.print(c + " ");
		}
	}
	
	@Test
	public void test55() {
		String str = "java AndroidA";
		String newStr = str.replace('A', 'B');
		System.out.println(str);//java AndroidA
		System.out.println(newStr);//java BndroidB
	}
	
	@Test
	public void test66() {
		String str = "java AndroidA Python";
		String[] array = str.split(" ");
		for (String string : array) {
			System.out.println(string);
		}
	}
	
	@Test
	public void test77() {
		String str = "java AndroidA";
		System.out.println(str.substring(5));//AndroidA
		System.out.println(str.substring(5, 8));//And
	}
	
	@Test
	public void test88() {
		String str = "   java AndroidA   ";
		System.out.println(str);//   java AndroidA   
		System.out.println(str.trim());//java AndroidA
	}
	
	@Test
	public void test444() {
		Studnet studnet = new Studnet();
		System.out.println(studnet.getCh());
	}
	
	@Test
	public void test45() {
		String str1 = "Hello";
		String str2 = "Java";
		String str3 = "Android";
		String str4 = "Python";
		String str = str1 + str2 + str3 + str4;
		
		StringBuilder builder = new StringBuilder(str1);//Hello
		builder.append(str2);//HelloJava
		builder.append(str3);//HelloJavaAndroid
		builder.append(str4);//HelloJavaPython
		System.out.println(builder);
		
		//链式编程
		StringBuilder builder2 = new StringBuilder(str1);
		builder2.append(str2).append(str3).append(str4);
		System.out.println(builder2);
	}
	
}
